package com.hobee.basic;

import com.hobee.basic.model.es.ClassType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

/**
 * @author junjie.li
 * @date 2020/7/29 15:33
 */
@Component
public class BasicApplicationRunner implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(BasicApplicationRunner.class);

    @Autowired
    private ElasticsearchRestTemplate template;

    @Autowired
    public BasicApplicationRunner(ElasticsearchRestTemplate template) {
        this.template = template;
    }

    @Override
    public void run(String... args) throws Exception {
        boolean exists = template.indexExists(ClassType.class);
        if (!exists) {
            LOGGER.info("classtype index 不存在,开始创建索引");
            boolean index = template.createIndex(ClassType.class);
            if (index) {
                LOGGER.info("classtype index 创建索引成功");
            }
            boolean mapping = template.putMapping(ClassType.class);
            if (mapping) {
                LOGGER.info("classtype mapping {}", template.getMapping(ClassType.class));
            }
        } else {
            LOGGER.info("classtype:type{},mapping{}", template.getMapping("classtype", "classtype"));
        }
    }
}
